home *** CD-ROM | disk | FTP | other *** search
- /********************************/
- /* */
- /* PixMapper code is */
- /* Copyright 1992, Dave Mark */
- /* All Rights Reserved */
- /* */
- /********************************/
-
- #include <QDOffscreen.h>
- #include <Picker.h>
- #include <GestaltEqu.h>
-
- #define kMBARResID 128
- #define kSleep 0L
- #define kMoveToFront (WindowPtr)-1L
- #define kEmptyString "\p"
- #define kEmptyTitle kEmptyString
- #define kVisible true
- #define kNoGoAway false
- #define kNilRefCon (long)nil
- #define kErrorAlertID 128
- #define kNilFilterProc nil
-
- #define kSquareSize 16
-
- #define kForegroundPICT 128
- #define kIgnored nil
- #define kUseMaxDepth 0
- #define kNoFlags (GWorldFlags)0
-
- #define mApple 128
- #define iAbout 1
-
- #define mFile 129
- #define iRedraw 1
- #define iQuit 3
-
- #define mColors 131
- #define iUseRGB 1
- #define iUseHSV 2
- #define iRed 4
- #define iGreen 5
- #define iBlue 6
- #define iHue 4
- #define iSaturation 5
- #define iBrightness 6
-
- // Big Debugging thanks to Stu Mark and Bill Mohler
-
- /*************/
- /* Globals */
- /*************/
-
- Boolean gDone;
- Boolean gIsRGB = true, gRandomReds = true, gRandomGreens = true, gRandomBlues = true;
- Boolean gRandomHue = true, gRandomSaturation = true, gRandomBrightness = true;
- WindowPtr gMainWindow;
- short gXBump = 1, gYBump = 1; // <--Try changing these numbers
- GWorldPtr gPictWorld, gSaveWorld, gSaveMixWorld;
- PixMapHandle gPixMapSave, gPixMapSaveMix, gPixMapPict;
- Rect gSavedFloaterRect, gPictWorldRect, gWorldRect;
- short gGlobalHue;
-
- /*
- * Exercise: Add update handling - When you get an update, regen the
- * screen (randomly), resave the rect behind the floater,
- * and redraw the floater...
- */
-
-
- /***************/
- /* Functions */
- /***************/
-
- void ToolboxInit( void );
- void MenuBarInit( void );
- Boolean HasGWorlds( void );
- void WindowInit( void );
- void PaintWindow( void );
- void RandomForeColor( void );
- void GWorldInit( void );
- GWorldPtr MakeGWorld( Rect *boundsPtr );
- void EventLoop( void );
- void DoEvent( EventRecord *eventPtr );
- void HandleMouseDown( EventRecord *eventPtr );
- void HandleMenuChoice( long menuChoice );
- void HandleAppleChoice( short item );
- void HandleFileChoice( short item );
- void HandleColorsChoice( short item );
- void MainLoop( void );
- void DrawFirstFloater( void );
- void MoveFloater( void );
- void CalcNewFloaterPosition( void );
- void DoError( Str255 errorString );
-
-
- /****************** main ***************************/
-
- void main( void )
- {
- ToolboxInit();
- MenuBarInit();
-
- if ( ! HasGWorlds() )
- DoError( "\pDeep GWorlds not supported by this machine!" );
-
- WindowInit();
- GWorldInit();
-
- DrawFirstFloater();
-
- EventLoop();
- }
-
-
- /****************** ToolboxInit *********************/
-
- void ToolboxInit( void )
- {
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( NULL );
- InitCursor();
- }
-
-
- /****************** MenuBarInit ***********************/
-
- void MenuBarInit( void )
- {
- Handle menuBar;
- MenuHandle menu;
-
- menuBar = GetNewMBar( kMBARResID );
- SetMenuBar( menuBar );
-
- menu = GetMHandle( mApple );
- AddResMenu( menu, 'DRVR' );
-
- DrawMenuBar();
- }
-
-
- /****************** HasGWorlds *****************/
-
- Boolean HasGWorlds( void )
- {
- long feature;
- OSErr err;
-
- err = Gestalt( gestaltQuickdrawFeatures, &feature );
-
- if ( err != noErr )
- DoError( "\pError calling Gestalt!" );
-
- if ( feature & 0x0004 )
- return true;
- else
- return false;
- }
-
-
- /****************** WindowInit ***********************/
-
- void WindowInit( void )
- {
- Rect r;
-
- r = screenBits.bounds;
- r.top += GetMBarHeight();
-
- gMainWindow = NewCWindow( nil, &r, kEmptyTitle,
- kVisible, plainDBox, kMoveToFront,
- kNoGoAway, kNilRefCon );
-
- SetPort( gMainWindow );
-
- PaintWindow();
- }
-
-
- /****************** PaintWindow ***********************/
-
- void PaintWindow( void )
- {
- Rect r;
- short row, col, numRows, numCols;
-
- SetPort( gMainWindow );
-
- r = gMainWindow->portRect;
-
- numCols = (r.right - r.left) / kSquareSize;
- if ( numCols != numCols/kSquareSize*kSquareSize )
- numCols++;
-
- numRows = (r.bottom - r.top) / kSquareSize;
- if ( numRows != numRows/kSquareSize*kSquareSize )
- numRows++;
-
- GetDateTime( (unsigned long *)(&randSeed) );
-
- gGlobalHue = Random();
-
- for ( row=0; row<numRows; row++ )
- for ( col=0; col<numCols; col++ )
- {
- r.top = row * kSquareSize;
- r.bottom = r.top + kSquareSize;
- r.left = col * kSquareSize;
- r.right = r.left + kSquareSize;
- RandomForeColor();
- PaintRect( &r );
- }
-
- ForeColor( blackColor );
- BackColor( whiteColor );
- }
-
-
- /****************** RandomForeColor ***********************/
-
- void RandomForeColor( void )
- {
- RGBColor color;
- HSVColor hsvColor;
-
- if ( gIsRGB )
- {
- if ( gRandomReds )
- color.red = Random();
- else
- color.red = 0;
-
- if ( gRandomGreens )
- color.green = Random();
- else
- color.green = 0;
-
- if ( gRandomBlues )
- color.blue = Random();
- else
- color.blue = 0;
-
- RGBForeColor( &color );
- }
- else
- {
- if ( gRandomHue )
- hsvColor.hue = Random();
- else
- hsvColor.hue = gGlobalHue;
-
- if ( gRandomSaturation )
- hsvColor.saturation = Random();
- else
- hsvColor.saturation = 65535;
-
- if ( gRandomBrightness )
- hsvColor.value = Random();
- else
- hsvColor.value = 65535;
-
- HSV2RGB( &hsvColor, &color );
- RGBForeColor( &color );
- }
- }
-
-
- /****************** GWorldInit ***********************/
-
- void GWorldInit( void )
- {
- PicHandle pic;
-
- pic = GetPicture( kForegroundPICT );
-
- if ( pic == nil )
- DoError( "\pError loading PICT..." );
-
- // Call HNoPurge() if your PICT is purgeable
-
- gPictWorldRect = (**pic).picFrame;
- OffsetRect( &gPictWorldRect, - gPictWorldRect.left, - gPictWorldRect.top );
-
- gWorldRect = gPictWorldRect;
- gWorldRect.bottom += 2;
- gWorldRect.right += 2;
-
- gPictWorld = MakeGWorld( &gPictWorldRect );
- gSaveWorld = MakeGWorld( &gWorldRect );
- gSaveMixWorld = MakeGWorld( &gWorldRect );
-
- gPixMapPict = GetGWorldPixMap( gPictWorld );
- gPixMapSave = GetGWorldPixMap( gSaveWorld );
- gPixMapSaveMix = GetGWorldPixMap( gSaveMixWorld );
-
- // Lock pixels before you draw or read from PixMap
- // Unlock when you are done. We're leaving the whole thing locked
- // to avoid the hassle. In a real program, do it right.
-
- if ( ! LockPixels( gPixMapPict ) )
- DoError( "\pLockPixels failed..." );
-
- if ( ! LockPixels( gPixMapSave ) )
- DoError( "\pLockPixels failed..." );
-
- if ( ! LockPixels( gPixMapSaveMix ) )
- DoError( "\pLockPixels failed..." );
-
- SetGWorld( gPictWorld, kIgnored );
-
- DrawPicture( pic, &gPictWorldRect );
- }
-
-
- /*********************************** MakeGWorld */
-
- GWorldPtr MakeGWorld( Rect *boundsPtr )
- {
- QDErr err;
- GWorldPtr newGWorld;
-
- err = NewGWorld( &newGWorld, kUseMaxDepth,
- boundsPtr, kIgnored, kIgnored, noNewDevice );
-
- // In the real world, call DisposeGWorld() when you are done with the GWorld...
-
- if ( err != noErr )
- DoError( "\pMy call to NewGWorld died! Bye..." );
-
- return( newGWorld );
- }
-
-
- /************************************* EventLoop */
-
- void EventLoop( void )
- {
- EventRecord event;
-
- gDone = false;
- while ( gDone == false )
- {
- if ( WaitNextEvent( everyEvent, &event, kSleep, NULL ) )
- DoEvent( &event );
- MoveFloater();
- }
- }
-
-
- /************************************* DoEvent */
-
- void DoEvent( EventRecord *eventPtr )
- {
- char theChar;
-
- switch ( eventPtr->what )
- {
- case mouseDown:
- HandleMouseDown( eventPtr );
- break;
- case keyDown:
- case autoKey:
- theChar = eventPtr->message & charCodeMask;
- if ( (eventPtr->modifiers & cmdKey) != 0 )
- HandleMenuChoice( MenuKey( theChar ) );
- break;
- case updateEvt:
- BeginUpdate( (WindowPtr)(eventPtr->message) );
- EndUpdate( (WindowPtr)(eventPtr->message) );
- break;
- }
- }
-
-
- /************************************* HandleMouseDown */
-
- void HandleMouseDown( EventRecord *eventPtr )
- {
- WindowPtr window;
- short thePart;
- long menuChoice;
-
- thePart = FindWindow( eventPtr->where, &window );
-
- switch ( thePart )
- {
- case inMenuBar:
- menuChoice = MenuSelect( eventPtr->where );
- HandleMenuChoice( menuChoice );
- break;
- case inSysWindow :
- SystemClick( eventPtr, window );
- break;
- }
- }
-
-
- /************************************* HandleMenuChoice */
-
- void HandleMenuChoice( long menuChoice )
- {
- short menu;
- short item;
-
- if ( menuChoice != 0 )
- {
- menu = HiWord( menuChoice );
- item = LoWord( menuChoice );
-
- switch ( menu )
- {
- case mApple:
- HandleAppleChoice( item );
- break;
- case mFile:
- HandleFileChoice( item );
- break;
- case mColors:
- HandleColorsChoice( item );
- break;
- }
- HiliteMenu( 0 );
- }
- }
-
-
- /************************************* HandleAppleChoice */
-
- void HandleAppleChoice( short item )
- {
- MenuHandle appleMenu;
- Str255 accName;
- short accNumber;
-
- switch ( item )
- {
- case iAbout:
- SysBeep( 20 );
- break;
- default:
- appleMenu = GetMHandle( mApple );
- GetItem( appleMenu, item, accName );
- accNumber = OpenDeskAcc( accName );
- break;
- }
- }
-
-
- /************************************* HandleFileChoice */
-
- void HandleFileChoice( short item )
- {
- switch ( item )
- {
- case iRedraw:
- PaintWindow();
- DrawFirstFloater();
- break;
- case iQuit:
- gDone = true;
- break;
- }
- }
-
-
- /************************************* HandleColorsChoice */
-
- void HandleColorsChoice( short item )
- {
- MenuHandle menu;
-
- menu = GetMenu( mColors );
-
- if ( item == iUseRGB )
- {
- gIsRGB = true;
-
- SetItem( menu, iRed, "\pRandom Reds" );
- SetItem( menu, iGreen, "\pRandom Greens" );
- SetItem( menu, iBlue, "\pRandom Blues" );
-
- CheckItem( menu, iUseRGB, true );
- CheckItem( menu, iUseHSV, false );
- CheckItem( menu, iRed, gRandomReds );
- CheckItem( menu, iGreen, gRandomGreens );
- CheckItem( menu, iBlue, gRandomBlues );
- }
- else if ( item == iUseHSV )
- {
- gIsRGB = false;
-
- SetItem( menu, iHue, "\pRandom Hue" );
- SetItem( menu, iSaturation, "\pRandom Saturation" );
- SetItem( menu, iBrightness, "\pRandom Brightness" );
-
- CheckItem( menu, iUseRGB, false );
- CheckItem( menu, iUseHSV, true );
- CheckItem( menu, iHue, gRandomHue );
- CheckItem( menu, iSaturation, gRandomSaturation );
- CheckItem( menu, iBrightness, gRandomBrightness );
- }
- else if ( gIsRGB )
- {
- switch ( item )
- {
- case iRed:
- gRandomReds = !gRandomReds;
- CheckItem( menu, iRed, gRandomReds );
- break;
- case iGreen:
- gRandomGreens = ! gRandomGreens;
- CheckItem( menu, iGreen, gRandomGreens );
- break;
- case iBlue:
- gRandomBlues = ! gRandomBlues;
- CheckItem( menu, iBlue, gRandomBlues );
- break;
- }
- }
- else
- {
- switch ( item )
- {
- case iHue:
- gRandomHue = !gRandomHue;
- CheckItem( menu, iHue, gRandomHue );
- break;
- case iSaturation:
- gRandomSaturation = ! gRandomSaturation;
- CheckItem( menu, iSaturation, gRandomSaturation );
- break;
- case iBrightness:
- gRandomBrightness = ! gRandomBrightness;
- CheckItem( menu, iBrightness, gRandomBrightness );
- break;
- }
- }
- PaintWindow();
- DrawFirstFloater();
- }
-
-
- /****************** DrawFirstFloater *********************/
-
- void DrawFirstFloater( void )
- {
- CopyBits( &(gMainWindow->portBits), (BitMap *)(*gPixMapSave),
- &gWorldRect, &gWorldRect, srcCopy, nil );
-
- gSavedFloaterRect = gPictWorldRect;
- OffsetRect( &gSavedFloaterRect, 1, 1 );
-
- CopyBits( (BitMap *)(*gPixMapPict), &(gMainWindow->portBits),
- &gPictWorldRect, &gSavedFloaterRect, transparent, nil );
- }
-
-
- /****************** MoveFloater *********************/
-
- void MoveFloater( void )
- {
- Rect r;
- RgnHandle newRgn, savedRgn, oldClip;
-
- CalcNewFloaterPosition();
-
- CopyBits( (BitMap *)(*gPixMapSave), (BitMap *)(*gPixMapSaveMix),
- &gWorldRect, &gWorldRect, srcCopy, nil );
-
- r = gPictWorldRect;
- OffsetRect( &r, gXBump + 1, gYBump + 1 );
-
- CopyBits( (BitMap *)(*gPixMapPict), (BitMap *)(*gPixMapSaveMix),
- &gPictWorldRect, &r, transparent, nil );
-
- r = gSavedFloaterRect;
- InsetRect( &r, -1, -1 );
-
- CopyBits( (BitMap *)(*gPixMapSaveMix), &(gMainWindow->portBits),
- &gWorldRect, &r, srcCopy, nil );
-
- OffsetRect( &gSavedFloaterRect, gXBump, gYBump );
-
- r = gSavedFloaterRect;
- InsetRect( &r, -1, -1 );
-
- CopyBits( &(gMainWindow->portBits), (BitMap *)(*gPixMapSaveMix),
- &r, &gWorldRect, srcCopy, nil );
-
- r = gWorldRect;
- OffsetRect( &r, -gXBump, -gYBump );
-
- CopyBits( (BitMap *)(*gPixMapSave), (BitMap *)(*gPixMapSaveMix),
- &gWorldRect, &r, srcCopy, nil );
-
- CopyBits( (BitMap *)(*gPixMapSaveMix), (BitMap *)(*gPixMapSave),
- &gWorldRect, &gWorldRect, srcCopy, nil );
- }
-
-
- /*********************************** CalcNewFloaterPosition */
-
- void CalcNewFloaterPosition( void )
- {
- Rect r;
-
- r = gSavedFloaterRect;
-
- OffsetRect( &r, gXBump, gYBump );
-
- if ( (r.left < gMainWindow->portRect.left) ||
- ( r.right > gMainWindow->portRect.right ) )
- gXBump *= -1;
-
- if ( (r.top < gMainWindow->portRect.top) ||
- ( r.bottom > gMainWindow->portRect.bottom ) )
- gYBump *= -1;
- }
-
-
- /***************** DoError ********************/
-
- void DoError( Str255 errorString )
- {
- ParamText( errorString, kEmptyString, kEmptyString, kEmptyString );
-
- StopAlert( kErrorAlertID, kNilFilterProc );
-
- ExitToShell();
- }